/**
 * Created by Celebration-Grey on 2017/4/7.
 */

weibo.register = {};

weibo.login = {};

weibo.reset = {};

weibo.register.init = function () {
    weibo.register.getSession();
    $('#user_nickname').focus(function () {
        $(this).parents(".info_list").find(".tips").html(weibo.register.nicknameFocusDom());
    });
    $('#user_nickname').blur(function () {
        var nickname = $(this).val();
        var pattern = /[@#\$%\^&\*]+/g;
        if (nickname == "" || nickname == undefined || nickname == null || pattern.test(nickname)) {
            $(this).parents(".info_list").find(".tips").html(weibo.register.nicknameErrorDom());
        }
        else {
            $(this).parents(".info_list").find(".tips").html(weibo.register.successDom());
        }
    });
    $('#user_email').focus(function () {
        $(this).parents(".info_list").find(".tips").html(weibo.register.emailFocusDom());
    });
    $("#user_email").blur(function () {
        var email = $(this).val();
        var pattern = /^[_a-zA-Z0-9\-\.]+@([\-_a-zA-Z0-9]+\.)+[a-zA-Z0-9]{2,3}$/;
        if (email == "" || email == undefined || email == null || !pattern.test(email)) {
            $(this).parents(".info_list").find(".tips").html(weibo.register.emailErrorDom());
        } else {
            $(this).parents(".info_list").find(".tips").html(weibo.register.successDom());
        }
    });
    var $btn = $("#send");
    $btn.click(function () {
        var email = $("#user_email").val();
        $("#user_email").attr('disabled','disabled');
        weibo.register.postEmail(email);
    });
    $("#user_email").keyup(function () {
        var email = $(this).val();
        var pattern = /^[_a-zA-Z0-9\-\.]+@([\-_a-zA-Z0-9]+\.)+[a-zA-Z0-9]{2,3}$/;
        if (email == "" || email == undefined || email == null || !pattern.test(email)) {
            $btn.addClass('W_btn_a_disable');
        } else {
            $btn.removeClass('W_btn_a_disable');
        }
    });
    $("#code").focus(function () {
        $(this).parents(".info_list").find(".tip").html(weibo.register.codeFocusDom());
    });
    $("#code").blur(function () {
        var code = $(this).val();
        var pattern = /^\d{6}$/;
        var $tips = $(this).parents(".info_list").find(".tip");
        if (code == "" || code == undefined || code == null || !pattern.test(code)) {
            $tips.html(weibo.register.codeErrorDom());
        } else {
            $tips.html(weibo.register.successDom());
        }
    });
    $("#user_password").focus(function () {
        $(this).parents(".info_list").find(".tips").html(weibo.register.passwordFocusDom());
    });
    $("#user_password").blur(function () {
        var password = $(this).val();
        var pattern = /\w{6,18}$/;
        var $tips = $(this).parents(".info_list").find(".tips");
        if (password == "" || password == undefined || password == null || !pattern.test(password)) {
            $tips.html(weibo.register.passwordErrorDom());
        } else {
            $tips.html(weibo.register.successDom());
        }
    });
    $("#re_password").focus(function () {
        $(this).parents(".info_list").find(".tips").html(weibo.register.repasswordFocusDom());
    });
    $("#re_password").blur(function () {
        var repassword = $(this).val();
        var password = $("#user_password").val();
        var $tips = $(this).parents(".info_list").find(".tips");
        if (repassword == password) {
            $tips.html(weibo.register.successDom());
        } else {
            $tips.html(weibo.register.repasswordErrorDom());
        }
    });
    $(".W_input").keyup(function () {
        var $btn_register = $("#btn_register");
        weibo.register.verify($btn_register);
    });
};

weibo.register.nicknameErrorDom = function () {
    var nicknameInfoString = '<p class="error"><span class="icon_rederrorS"></span>请输入合法的昵称</p>';
    var $nicknameInfoDom = $(nicknameInfoString);
    return $nicknameInfoDom;
};

weibo.register.nicknameFocusDom = function () {
    var nicknameInfoString = '<p class="notice"><span class="icon_warnS"></span>请输入昵称</p>';
    var $nicknameInfoDom = $(nicknameInfoString);
    return $nicknameInfoDom;
};

weibo.register.emailErrorDom = function () {
    var domString = '<p class="error"><span class="icon_rederrorS"></span>请输入正确的邮箱</p>';
    var $Dom = $(domString);
    return $Dom;
};

weibo.register.emailFocusDom = function () {
    var domString = '<p class="notice"><span class="icon_warnS"></span>请输入邮箱</p>';
    var $Dom = $(domString);
    return $Dom;
};

weibo.register.codeErrorDom = function () {
    var domString = '<p class="error"><span class="icon_rederrorS"></span>请输入6位数字验证码</p>';
    var $Dom = $(domString);
    return $Dom;
};

weibo.register.codeFocusDom = function () {
    var domString = '<p class="notice"><span class="icon_warnS"></span>请输入验证码</p>';
    var $Dom = $(domString);
    return $Dom;
};

weibo.register.passwordErrorDom = function () {
    var domString = '<p class="error"><span class="icon_rederrorS"></span>请输入6~18位数字、字母或常用符号，字母区分大小写</p>';
    var $Dom = $(domString);
    return $Dom;
};

weibo.register.passwordFocusDom = function () {
    var domString = '<p class="notice"><span class="icon_warnS"></span>请输入6~18位数字、字母或常用符号，字母区分大小写</p>';
    var $Dom = $(domString);
    return $Dom;
};

weibo.register.repasswordFocusDom = function () {
    var domString = '<p class="notice"><span class="icon_warnS"></span>两次密码输入请保持一致</p>';
    var $Dom = $(domString);
    return $Dom;
};

weibo.register.repasswordErrorDom = function () {
    var domString = '<p class="error"><span class="icon_rederrorS"></span>两次输入的密码不一致</p>';
    var $Dom = $(domString);
    return $Dom;
};

weibo.register.successDom = function () {
    var successString = '<p class="success"><span class="icon_succS"></span></p>';
    var $success = $(successString);
    return $success;
};

weibo.register.infoDom = function (type, message) {
    var domString = '';
    if (type == 200) {
        domString = '<div class="success"><p><span class="icon_succS"></span>' + message + '</p></div>';
    }
    else {
        domString = '<div class="error"><p><span class="icon_rederrorS"></span>' + message + '</p></div>';
    }
    var $Dom = $(domString);
    return $Dom;
};

weibo.register.verify = function ($btn_register) {
    var nickname = $("#user_nickname").val();
    var password = $("#user_password").val();
    var repassword = $("#re_password").val();
    var email = $("#user_email").val();
    var code = $("#code").val();
    var phpsessionid = $("#php_session_id").val();
    var pattern_nickname = /[@#\$%\^&\*]+/g;
    var pattern_password = /\w{6,18}$/;
    var pattern_email = /^[_a-zA-Z0-9\-\.]+@([\-_a-zA-Z0-9]+\.)+[a-zA-Z0-9]{2,3}$/;
    var pattern_code = /^\d{6}$/;
    if (!pattern_nickname.test(nickname) &&
        pattern_password.test(password) && pattern_email.test(email) && pattern_code.test(code) && password == repassword) {
        $btn_register.removeClass('W_btn_big_dis');
        $btn_register.unbind('click');
        $btn_register.click(function () {
            weibo.register.registerRequest(nickname, email, password, repassword, phpsessionid, code);
        });
        return true;
    } else {
        $btn_register.addClass('W_btn_big_dis');
        $btn_register.unbind('click');
        return false;
    }
    // return (!pattern_nickname.test(nickname)) && pattern_password.test(password) && pattern_email.test(email) && pattern_code.test(code);
};

weibo.register.waiting = function () {
    var $send = $("#send");
    var $span = $send.find("span");
    $send.addClass("W_btn_a_disable");
    var second = 60;
    var timer = setInterval(function () {
        $span.html(second + "s后再操作");
        second--;
        if (second < 0) {
            clearInterval(timer);
            $('#user_email').removeAttr('disabled');
            $send.removeClass('W_btn_a_disable');
            $span.html("获取验证码");
        }
    }, 1000);
};

weibo.register.getSession = function () {
        $.ajax({
            type: 'GET',
            url: host + 'weibo_session',
            data: {},
            xhrFields: {
                withCredentials: true
            },
            success: function (data) {
                var $data = data;
                if ($data.status == 200) {
                    $("#php_session_id").val($data.data);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.log(XMLHttpRequest);
                console.log(textStatus);
                console.log(errorThrown);
            }
        });
};

weibo.register.postEmail = function (email) {
    weibo.register.waiting();
    $.ajax({
        type: 'POST',
        url: host + 'weibo_register_request',
        data: {
            'email': email
        },
        xhrFields: {
            withCredentials: true
        },
        success: function (data) {
            var $data = data;
            $("#register-info").html(weibo.register.infoDom($data.status, $data.message));
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log(XMLHttpRequest);
            console.log(textStatus);
            console.log(errorThrown);
        }
    });
};

weibo.register.registerRequest = function (nickname, email, password, repassword, phpsessionid, code) {
    $.ajax({
        type: 'POST',
        url: host + 'weibo_register',
        data: {
            'user_nickname': nickname,
            'user_email': email,
            'user_password': password,
            're_password': repassword,
            'php_session_id': phpsessionid,
            'code': code
        },
        xhrFields: {
            withCredentials: true
        },
        success: function (data) {
            var $data = data;
            $("#register-info").html(weibo.register.infoDom($data.status, $data.message));
            if($data.status == 200){
                setTimeout(function () {
                    window.location.href = "login.html";
                }, 1000);
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log(XMLHttpRequest);
            console.log(textStatus);
            console.log(errorThrown);
        }
    });
};

weibo.login.init = function () {
    if(localStorage.getItem('user_info') != null){
        window.location.href = 'home.html';
    }
    var $btn_login = $('#btn_login');
    $(".W_input").keyup(function () {
        weibo.login.verify($btn_login);
    });
};

weibo.login.verify = function ($btn_login) {
    var password = $("#user_password").val();
    var email = $("#user_email").val();
    var pattern_password = /\w{6,18}$/;
    var pattern_email = /^[_a-zA-Z0-9\-\.]+@([\-_a-zA-Z0-9]+\.)+[a-zA-Z0-9]{2,3}$/;
    if (pattern_email.test(email) && pattern_password.test(password)) {
        $btn_login.removeClass('W_btn_big_dis');
        $btn_login.unbind('click');
        $btn_login.click(function () {
            weibo.login.loginRequest(email, password);
        });
    } else {
        $btn_login.addClass('W_btn_big_dis');
        $btn_login.unbind('click');
    }
};

weibo.login.loginRequest = function (email, password) {
    $.ajax({
        type: 'POST',
        url: host + 'weibo_login',
        data: {
            'user_email': email,
            'user_password': password
        },
        xhrFields: {
            withCredentials: true
        },
        success: function (data) {
            var $data = data;
            $("#login-info").html(weibo.register.infoDom($data.status, $data.message));
            if ($data.status == 200) {
                localStorage.clear();
                setTimeout(function () {
                    window.location.href = "home.html";
                }, 1000);
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log(XMLHttpRequest);
            console.log(textStatus);
            console.log(errorThrown);
        }
    });
};

weibo.reset.init = function () {
    weibo.register.getSession();
    $('#user_email').focus(function () {
        $(this).parents(".info_list").find(".tips").html(weibo.register.emailFocusDom());
    });
    $("#user_email").blur(function () {
        var email = $(this).val();
        var pattern = /^[_a-zA-Z0-9\-\.]+@([\-_a-zA-Z0-9]+\.)+[a-zA-Z0-9]{2,3}$/;
        if (email == "" || email == undefined || email == null || !pattern.test(email)) {
            $(this).parents(".info_list").find(".tips").html(weibo.register.emailErrorDom());
        } else {
            $(this).parents(".info_list").find(".tips").html(weibo.register.successDom());
        }
    });
    $("#user_email").keyup(function () {
        var email = $(this).val();
        var pattern = /^[_a-zA-Z0-9\-\.]+@([\-_a-zA-Z0-9]+\.)+[a-zA-Z0-9]{2,3}$/;
        if (email == "" || email == undefined || email == null || !pattern.test(email)) {
            $btn.addClass('W_btn_a_disable');
        } else {
            $btn.removeClass('W_btn_a_disable');
        }
    });
    $("#code").focus(function () {
        $(this).parents(".info_list").find(".tip").html(weibo.register.codeFocusDom());
    });
    $("#code").blur(function () {
        var code = $(this).val();
        var pattern = /^\d{6}$/;
        var $tips = $(this).parents(".info_list").find(".tip");
        if (code == "" || code == undefined || code == null || !pattern.test(code)) {
            $tips.html(weibo.register.codeErrorDom());
        } else {
            $tips.html(weibo.register.successDom());
        }
    });
    $("#user_password").focus(function () {
        $(this).parents(".info_list").find(".tips").html(weibo.register.passwordFocusDom());
    });
    $("#user_password").blur(function () {
        var password = $(this).val();
        var pattern = /\w{6,18}$/;
        var $tips = $(this).parents(".info_list").find(".tips");
        if (password == "" || password == undefined || password == null || !pattern.test(password)) {
            $tips.html(weibo.register.passwordErrorDom());
        } else {
            $tips.html(weibo.register.successDom());
        }
    });
    $("#re_password").focus(function () {
        $(this).parents(".info_list").find(".tips").html(weibo.register.repasswordFocusDom());
    });
    $("#re_password").blur(function () {
        var repassword = $(this).val();
        var password = $("#user_password").val();
        var $tips = $(this).parents(".info_list").find(".tips");
        if (repassword == password) {
            $tips.html(weibo.register.successDom());
        } else {
            $tips.html(weibo.register.repasswordErrorDom());
        }
    });
    var $btn = $("#send");
    $btn.click(function () {
        var email = $("#user_email").val();
        weibo.reset.resetRequest(email);
    });
    var $btn_reset = $('#btn_reset');
    $(".W_input").keyup(function () {
        weibo.reset.verify($btn_reset);
    });
};

weibo.reset.verify = function ($btn_reset) {
    var password = $("#user_password").val();
    var repassword = $("#re_password").val();
    var email = $("#user_email").val();
    var code = $("#code").val();
    var pattern_password = /\w{6,18}$/;
    var pattern_email = /^[_a-zA-Z0-9\-\.]+@([\-_a-zA-Z0-9]+\.)+[a-zA-Z0-9]{2,3}$/;
    var pattern_code = /^\d{6}$/;
    if (pattern_email.test(email) && pattern_password.test(password) && password == repassword && pattern_code.test(code)) {
        $btn_reset.removeClass('W_btn_big_dis');
        $btn_reset.unbind('click');
        $btn_reset.click(function () {
            $(this).addClass('W_btn_big_dis');
            weibo.reset.resetPassword(email, code, password, repassword);
        });
    } else {
        $btn_reset.addClass('W_btn_big_dis');
        $btn_reset.unbind('click');
    }
};

weibo.reset.resetRequest = function (email) {
    weibo.register.waiting();
    $.ajax({
        type: 'POST',
        url: host + 'weibo_reset_request',
        data: {
            'user_email': email
        },
        xhrFields: {
            withCredentials: true
        },
        success: function (data) {
            var $data = data;
            $("#reset-info").html(weibo.register.infoDom($data.status, $data.message));
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log(XMLHttpRequest);
            console.log(textStatus);
            console.log(errorThrown);
        }
    });
};

weibo.reset.resetPassword = function (email, code, password, repassword) {
    $.ajax({
        type: 'POST',
        url: host + 'weibo_reset',
        data: {
            'user_email': email,
            'code': code,
            'user_password': password,
            're_password': repassword
        },
        xhrFields: {
            withCredentials: true
        },
        success: function (data) {
            var $data = data;
            $("#reset-info").html(weibo.register.infoDom($data.status, $data.message));
            if ($data.status != 200) {
                $('#btn_reset').removeClass('W_btn_big_dis');
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log(XMLHttpRequest);
            console.log(textStatus);
            console.log(errorThrown);
        }
    });
};